package com.itheima.account.service.impl;


import com.itheima.account.dao.AccountDao;
import com.itheima.account.pojo.Account;
import com.itheima.account.service.DealService;

/**
 * @Description：交易接口实现
 */
public class DealServiceImpl implements DealService {

    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    //交易业务
    public Integer doDeal(Float money, Integer payAccountId, Integer receiptAccountId) {
        int flag = 0;
        //从支付账户扣款
        flag = accountDao.dealMoney(payAccountId, -money);
        if (flag == 0) {
            throw new RuntimeException("交易不成功");
        }


        //给接收账户加余额
        flag = flag + accountDao.dealMoney(receiptAccountId, money);
        if (flag == 1) {
            throw new RuntimeException("交易不成功");
        }
        return flag;
    }

    @Override
    //打印账户信息
    public void printAccountInfo(Integer payId, Integer rePayId) {
        //根据付款账户id查询账户对象
        Account payAccount = accountDao.findAccountById(payId);
        System.out.println("支付者账号余额：" + payAccount.getMoney());
        //根据付款账户id查询账户对象
        Account receiptAccount = accountDao.findAccountById(rePayId);
        System.out.println("收款者账户余额：" + receiptAccount.getMoney());

        //计算两个账户的平衡和
        Double sumMoney = payAccount.getMoney() + receiptAccount.getMoney();
        System.out.println("两人账户总余额：" + sumMoney);
    }


}